/**
 * 
 */
package com.youxin.cpcenter.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.youxin.busfoundation.common.BusinessDataException;
import com.youxin.cpcenter.dao.TbEntMemberIdentifyCapitalMapper;
import com.youxin.cpcenter.model.TbEntMemberIdentifyCapital;
import com.youxin.cpcenter.service.CouponService;

/**
 * @description 
 * @author yingde.cao
 * @date 2017年12月12日
 * @version 1.0
 */
@Service("CouponService")
public class CouponServiceImpl implements CouponService{

	@Autowired
	private TbEntMemberIdentifyCapitalMapper tbEntMemberIdentifyCapitalMapper;
	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.CouponService#isCanConsumer()
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public boolean isCanConsumer(long entIdentity,BigDecimal money) {
		TbEntMemberIdentifyCapital entMemberIdentifyCapital=tbEntMemberIdentifyCapitalMapper.selectByPrimaryKey(entIdentity);
		if(entMemberIdentifyCapital==null)
		{
			throw new BusinessDataException("系统异常,没有用户认证企业信息");
		}
		if(entMemberIdentifyCapital.getCoupon().compareTo(money)>=0)
		{
			return true;
		}
		return false;
	}
	
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean isCanConsumer(long entIdentity) {
		TbEntMemberIdentifyCapital entMemberIdentifyCapital=tbEntMemberIdentifyCapitalMapper.selectByPrimaryKey(entIdentity);
		if(entMemberIdentifyCapital==null)
		{
			throw new BusinessDataException("系统异常,没有用户认证企业信息");
		}
		if(entMemberIdentifyCapital.getCoupon().compareTo(new BigDecimal(0))>0)
		{
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.CouponService#increaseCoupon(long, java.math.BigDecimal)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void increaseCoupon(long entIdentity, BigDecimal money) {
		TbEntMemberIdentifyCapital entMemberIdentifyCapital=new TbEntMemberIdentifyCapital();
		entMemberIdentifyCapital.setId(entIdentity);
		entMemberIdentifyCapital.setCoupon(money);
		entMemberIdentifyCapital.setUpdateTime(System.currentTimeMillis());
		int rowEffected=tbEntMemberIdentifyCapitalMapper.increaseCoupon(entMemberIdentifyCapital);
		if(rowEffected!=1)
		{
			throw new BusinessDataException("充值失败");
		}
	}

	/* (non-Javadoc)
	 * @see com.youxin.cpcenter.service.CouponService#decreaseCoupon(long, java.math.BigDecimal)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void decreaseCoupon(long entIdentity, BigDecimal money) {
		TbEntMemberIdentifyCapital entMemberIdentifyCapital=new TbEntMemberIdentifyCapital();
		entMemberIdentifyCapital.setId(entIdentity);
		entMemberIdentifyCapital.setCoupon(money);
		entMemberIdentifyCapital.setUpdateTime(System.currentTimeMillis());

		int rowEffected=tbEntMemberIdentifyCapitalMapper.decreaseCoupon(entMemberIdentifyCapital);
		if(rowEffected!=1)
		{
			throw new BusinessDataException("金券不足");
		}		
	}

}
